package com.nit.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nit.server.common.QueryPageParam;
import com.nit.server.common.Result;
import com.nit.server.common.UseApplyParam;
import com.nit.server.entity.Useapply;
import com.nit.server.entity.Usegoods;
import com.nit.server.entity.Userecord;
import com.nit.server.service.UseapplyService;
import com.nit.server.service.UsegoodsService;
import com.nit.server.service.UserecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * 领用物品管理
 */
@RestController
@RequestMapping("/usegoods")
public class UsegoodsController {
    @Autowired
    private UsegoodsService usegoodsService;
    @Autowired
    private UseapplyService useapplyService;
    @Autowired
    private UserecordService userecordService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Usegoods usegoods){
        return usegoodsService.save(usegoods)?Result.success():Result.fail();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Usegoods usegoods){
        return usegoodsService.updateById(usegoods)?Result.success():Result.fail();
    }
    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String usegoodsId){
        return usegoodsService.removeById(usegoodsId)?Result.success():Result.fail();
    }
    @PostMapping("/listPage")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result listPage(@RequestBody QueryPageParam pageParam){
        System.out.println(pageParam);
        String name = (String)pageParam.getParams().get("usegoodsName");
        String cId = (String)pageParam.getParams().get("cId");
        Page<Usegoods> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());
        QueryWrapper<Usegoods> queryWrapper = new QueryWrapper();
        queryWrapper.apply("a.c_id = b.c_id ");
        if (StringUtils.isNotBlank(cId)){
            queryWrapper.eq("a.c_id",cId);
        }
        if (StringUtils.isNotBlank(name)){
            queryWrapper.like("a.usegoods_name",name);
        }
//        IPage result = userService.pageC(page);
        IPage result = usegoodsService.pageCC(page,queryWrapper);

        return Result.success(result.getTotal(), result.getRecords());
    }
    @GetMapping("/list")
    public Result list(){
        List list = usegoodsService.list();

        return Result.success(list);
    }
    //加仓
    @PostMapping("/addsave")
    public Result addsave(@RequestBody Usegoods usegoods){
//        System.out.println(record);
        Usegoods usegoods1 = usegoodsService.getById(usegoods.getUsegoodsId());
        int n = usegoods.getUsegoodsCount();
        int newnum = n+usegoods1.getUsegoodsCount();
        usegoods1.setUsegoodsCount(newnum);
        return usegoodsService.updateById(usegoods1)?Result.success():Result.fail();
    }
    //领用
    @PostMapping("/usesave")
    public Result usesave(@RequestBody UseApplyParam param){
        System.out.println(param);
        int usegoodsId = param.getUsegoodsId();
        String usegoodsCount = param.getUsegoodsCount();
        String useinfo = param.getUseinfo();
        int userID = param.getUserId();

        Useapply useapply = new Useapply();
        useapply.setUsegoodsId(usegoodsId);
        useapply.setUseapplyCount(Integer.valueOf(usegoodsCount));
        useapply.setUseapplyInfo(useinfo);
        useapply.setUserId(userID);

        return useapplyService.save(useapply)?Result.success():Result.fail();
    }
}
